package com.baylor.server.business.util.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class UploadUtil {
	private static String gifdir;
	private static String targetPath;

	/**
	 * 构建目录
	 * 
	 * @param pathdir
	 *            目录的全路径
	 * @throws Exception
	 */
	public static void createDir(String pathdir) throws Exception {
		try {
			File dir = new File(pathdir);
			if (!dir.exists()) {
				dir.mkdirs();
			}
		} catch (Exception e) {
//			System.err.println(new Date() + ":" + e.getLocalizedMessage());
			System.out.println(1);
		}
	}

	/**
	 * 上传文件*(�?��上传)
	 * @param uploadfile  上传文件�?
	 * @param targetpath 上传后的文件全路径名
	 * @throws Exception
	 */
	public static void upload(File uploadfile, String targetpath)
			throws Exception {
		try {
			FileInputStream fin = new FileInputStream(uploadfile);
			FileOutputStream fout = new FileOutputStream(targetpath);
			byte[] buf = new byte[20480];
			int bufsize = 0;
			while ((bufsize = fin.read(buf, 0, buf.length)) != -1) {
				fout.write(buf, 0, bufsize);
			}
			fin.close();
			fout.close();

		} catch (Exception e) {
//			System.err.println(new Date() + ":" + e.getLocalizedMessage());
			System.out.println(1);
		}
	}

	/**
	 * 
	 * @param tempdir
	 * @param fileName
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static File upload(String tempdir, String fileName, InputStream in)
			throws Exception {
		File file = new File(tempdir + "/" + fileName);
		try {
			FileOutputStream fout = new FileOutputStream(file);
			byte[] buf = new byte[20480];
			int bufsize = 0;
			while ((bufsize = in.read(buf, 0, buf.length)) != -1) {
				fout.write(buf, 0, bufsize);
			}
			in.close();
			fout.close();

		} catch (Exception e) {
//			System.err.println(new Date() + ":" + e.getLocalizedMessage());
			System.out.println(1);
		}
		return file;
	}

	/**
	 * 获得文件后缀
	 * 
	 * @param filename
	 * @return
	 */
	public static String getExt(String filename) {
		return filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
	}

	/**
	 * 压缩处理上传图片
	 * 
	 * @param inputDir       输入图路�?
	 * @param outputDir      输出图路�?
	 * @param inputFileName  输入图文件名
	 * @param outputFileName 输出图文件名
	 * @param width          设置图片长宽
	 * @param height
	 * @param gp             是否是等比缩�?标记
	 */
	public static boolean compress(String inputDir, String outputDir,
			String inputFileName, String outputFileName, int width, int height,
			boolean gp ,int rf) {
		if ("gif".equalsIgnoreCase(UploadUtil.getExt(inputFileName))) {
			System.out.println("gif");
		} else {
			ImageSpek.compressPic(inputDir, outputDir, inputFileName,
					outputFileName, width, height, gp ,rf);
		}

		return true;
	}

	/**
	 * 
	 * @param tempFile
	 * @param fileName
	 * @param targetDir
	 * @param newWidth  压缩宽
	 * @param newHeight 压缩高
	 * @param minWidth  缩略宽
	 * @param minHeight 缩略高
	 * @return
	 */
	public static boolean uploadZip(String tempFile, 
			String fileName, String targetDir,int newWidth, int newHeight, int minWidth, int minHeight) {
		boolean issuccee = false;
		File file = new File(tempFile);
		if (!file.exists()) {// 文件是否上传成功
			return issuccee;
		}
		targetPath = targetDir;
		// 上传的文件目标全路径�?
		String filepath = targetDir + "/" + fileName;
		String xiao = targetDir + "/thumb_" + fileName;
		try {
			// 构建目录结构
			UploadUtil.createDir(targetDir);
//			UploadUtil.createDir(gifdir);
			Image img = ImageIO.read(file);
			int width = img.getWidth(null);
			int height = img.getHeight(null);
			if (width > newWidth) {
				issuccee = UploadUtil.compress(file.getParent(), targetDir,
						file.getName(), fileName, newWidth, newHeight, true ,0);
			} else {
				UploadUtil.upload(file, filepath);
			}
			if (width <= minWidth && height <= minHeight) {
				UploadUtil.upload(file, xiao);
			} else {
				issuccee = UploadUtil.compress(file.getParent(), targetDir,
						file.getName(), "thumb_" + fileName, minWidth,
						minHeight, true ,0);
			}
//			file.delete();
			issuccee = true;
		} catch (Exception e) {
			e.printStackTrace();
			return issuccee;
		}
		return issuccee;
	}
	
	/**
	 * 
	 * @param tempFile
	 * @param fileName
	 * @param targetDir
	 * @param minWidth  缩略宽
	 * @param minHeight 缩略高
	 * @return
	 */
	public static boolean uploadZip(String tempFile, 
			String fileName, String targetDir,int minWidth, int minHeight) {
		boolean issuccee = false;
		File file = new File(tempFile);
		if (!file.exists()) {// 文件是否上传成功
			return issuccee;
		}
		targetPath = targetDir;
		// 上传的文件目标全路径�?
		String filepath = targetDir + "/" + fileName;
		String xiao = targetDir + "thumb_" + fileName;
		try {
			// 构建目录结构
			UploadUtil.createDir(targetDir);
//			UploadUtil.createDir(gifdir);
			Image img = ImageIO.read(file);
			int width = img.getWidth(null);
			int height = img.getHeight(null);
//			if (width > newWidth) {
//				issuccee = UploadUtil.compress(file.getParent(), targetDir,
//						file.getName(), fileName, newWidth, newHeight, true);
//			} else {
//				UploadUtil.upload(file, filepath);
//			}
			if (width <= minWidth && height <= minHeight) {
				UploadUtil.upload(file, xiao);
			} else {
				issuccee = UploadUtil.compress(file.getParent(), targetDir,
						file.getName(), "thumb_" + fileName, minWidth,
						minHeight, true ,0);
			}
//			file.delete();
			issuccee = true;
		} catch (Exception e) {
			e.printStackTrace();
			return issuccee;
		}
		return issuccee;
	}
	
	/**
	 * 
	 * @param tempFile
	 * @param fileName
	 * @param targetDir
	 * @param thumbName 缩略前缀
	 * @param minWidth  缩略宽
	 * @param minHeight 缩略高
	 * @return
	 */
	public static String uploadZip(String tempFile, 
			String fileName, String targetDir, String thumbName,int minWidth, int minHeight) {
		String resultName = "";
		File file = new File(tempFile);
		if (!file.exists()) {// 文件是否上传成功
			return "";
		}
		targetPath = targetDir;
		// 上传的文件目标全路径�?
		String filepath = targetDir + "/" + fileName;
		String xiao = targetDir + "/thumb_" + fileName;
		try {
			// 构建目录结构
			UploadUtil.createDir(targetDir);
			Image img = ImageIO.read(file);
			int width = img.getWidth(null);
			int height = img.getHeight(null);
			if (width <= minWidth && height <= minHeight) {
				UploadUtil.upload(file, xiao);
			} else {
				UploadUtil.compress(file.getParent(), targetDir,
						file.getName(), thumbName + fileName, minWidth,
						minHeight, true ,0);
				resultName = thumbName + fileName;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return resultName;
	}
	
	
	/**
	 * 
	 * @param tempFile
	 * @param fileName
	 * @param targetDir
	 * @param minWidth  缩略宽
	 * @param minHeight 缩略高
	 * @return
	 */
	public static String uploadCover(String tempFile, 
			String fileName, String targetDir, int minWidth, int minHeight) {
		String resultName = "";
		File file = new File(tempFile);
		if (!file.exists()) {// 文件是否上传成功
			return "";
		}
		targetPath = targetDir;
		// 上传的文件目标全路径�?
		String filepath = targetDir + "/" + fileName;
//		String xiao = targetDir + "/thumb_" + fileName;
		try {
			// 构建目录结构
			UploadUtil.createDir(targetDir);
			Image img = ImageIO.read(file);
			int width = img.getWidth(null);
			int height = img.getHeight(null);
			if (width <= minWidth && height <= minHeight) {
				resultName = fileName;
			} else {
				UploadUtil.compress(file.getParent(), targetDir,
						file.getName(),  fileName, minWidth,
						minHeight, true ,0);
				resultName = fileName;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return resultName;
	}
	
	/**
	 * 
	 * @param tempFile
	 * @param fileName
	 * @param targetDir
	 * @param minWidth  缩略宽
	 * @param minHeight 缩略高
	 * @param rateFlag  判断以哪种比例比例缩放(默认0为以比率大的进行比缩放,1为以宽比例缩放，2为以高比例缩放)
	 * @return
	 */
	public static String uploadCover(String tempFile, 
			String fileName, String targetDir, int minWidth, int minHeight, int rateFlag) {
		String resultName = "";
		File file = new File(tempFile);
		if (!file.exists()) {// 文件是否上传成功
			return "";
		}
		targetPath = targetDir;
		// 上传的文件目标全路径�?
		String filepath = targetDir + "/" + fileName;
//		String xiao = targetDir + "/thumb_" + fileName;
		try {
			// 构建目录结构
			UploadUtil.createDir(targetDir);
			Image img = ImageIO.read(file);
			int width = img.getWidth(null);
			int height = img.getHeight(null);
			if (width <= minWidth && height <= minHeight) {
				resultName = fileName;
			} else {
				UploadUtil.compress(file.getParent(), targetDir,
						file.getName(),  fileName, minWidth,
						minHeight, true ,rateFlag);
				resultName = fileName;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return resultName;
	}
	
	public static void main(String[] args) {
		String ss = "C:/Users/lenovo/Desktop/123/1.jpg";
		String s1 = "11.jpg";
		String s2 = "22.jpg";
		
		String m = "C:/Users/lenovo/Desktop/123";
//		UploadUtil.uploadZip(ss, s, m,"andriod_", 120, 120);
//		String mmmm = UploadUtil.uploadZip(ss, s, m, "iphone", 120, 120);
//		System.out.println(mmmm);
		
//		String u = "C:/Users/lenovo/Desktop/1111";
//		getSubFiles(u);
		
//		uploadCover(ss, s1, m, 160, 119, 0);
		uploadCover(ss, s2, m, 321, 1, 1);
		
		
	}
	
	
	static void getSubFiles(String u) {
		File baseDir = new File(u);
		try {
			File[] tmp = baseDir.listFiles();
			for (int i = 0; i < tmp.length; i++) {
				File file = new File(u + File.separator + tmp[i].getName());
				uploadCover(u + File.separator + tmp[i].getName(),tmp[i].getName() , u, 300, 300);
			}
		} catch (Exception e) {

		}
	}
	
	
}
